﻿// RankingSejmowy 1.0 - Polish parliment statistics for the masses.
// Copyright (C) 2010 Jarosław Dubrownik
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//  
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
using System.Linq;
using NHibernate;
using NHibernate.Criterion;
using RankingSejmowy.Model;

namespace RankingSejmowy.DAL.Statistics
{
    public class DeputyStatisticsRepository : Repository<DeputyStatistic>, IDeputyStatisticsRepository
    {
        #region IDeputyStatisticsRepository Members

        public DeputyStatistic[] GetTopOpportunists(int cadenceNum, IStatelessSession session)
        {
            var criteria = GetCadenceCriteria(cadenceNum, session)
                .Add(Restrictions.Eq("StatisticType", EStatisticType.TopOportunists))
                .AddOrder(Order.Asc("Position"))
                .SetMaxResults(10);
            return criteria.List<DeputyStatistic>().ToArray();
        }

        public DeputyStatistic[] GetTopPartyOpportunists(int cadenceNum, string partyName, IStatelessSession session)
        {
            var criteria = GetCadenceAndPartyCriteria(cadenceNum, partyName, session)
                .Add(Restrictions.Eq("StatisticType", EStatisticType.TopPartyOportunists))
                .AddOrder(Order.Asc("Position"))
                .SetMaxResults(10);
            return criteria.List<DeputyStatistic>().ToArray();
        }

        public DeputyStatistic[] GetTopNonconformists(int cadenceNum, IStatelessSession session)
        {
            var criteria = GetCadenceCriteria(cadenceNum, session)
                .Add(Restrictions.Eq("StatisticType", EStatisticType.TopNonconformists))
                .AddOrder(Order.Asc("Position"))
                .SetMaxResults(10);
            return criteria.List<DeputyStatistic>().ToArray();
        }

        public DeputyStatistic[] GetTopPartyNonconformists(int cadenceNum, string partyName, IStatelessSession session)
        {
            var criteria = GetCadenceAndPartyCriteria(cadenceNum, partyName, session)
                .Add(Restrictions.Eq("StatisticType", EStatisticType.TopPartyNonconformists))
                .AddOrder(Order.Asc("Position"))
                .SetMaxResults(10);
            return criteria.List<DeputyStatistic>().ToArray();
        }

        public DeputyStatistic[] GetTopUndecided(int cadenceNum, IStatelessSession session)
        {
            var criteria = GetCadenceCriteria(cadenceNum, session)
                .Add(Restrictions.Eq("StatisticType", EStatisticType.TopUndecided))
                .AddOrder(Order.Asc("Position"))
                .SetMaxResults(10);
            return criteria.List<DeputyStatistic>().ToArray();
        }

        public DeputyStatistic[] GetTopPartyUndecied(int cadenceNum, string partyName, IStatelessSession session)
        {
            var criteria = GetCadenceAndPartyCriteria(cadenceNum, partyName, session)
                .Add(Restrictions.Eq("StatisticType", EStatisticType.TopPartyUndecided))
                .AddOrder(Order.Asc("Position"))
                .SetMaxResults(10);
            return criteria.List<DeputyStatistic>().ToArray();
        }

        #endregion

        private ICriteria GetCadenceCriteria(int cadenceNum, IStatelessSession session)
        {
            return CreateCriteria(session)
                .CreateAlias("Cadence", "c")
                .Add(Restrictions.Eq("c.CadenceNum", cadenceNum));
        }

        private ICriteria GetCadenceAndPartyCriteria(int cadenceNum, string partyName, IStatelessSession session)
        {
            return CreateCriteria(session)
                .CreateAlias("Cadence", "c")
                .CreateAlias("Party", "p")
                .Add(Restrictions.Eq("c.CadenceNum", cadenceNum))
                .Add(Restrictions.Eq("p.Name", partyName));
        }
    }
}